package com.cw.service.impl;

import cn.hutool.crypto.*;
import com.baomidou.mybatisplus.core.conditions.query.*;
import com.cw.common.api.ResultObject;
import com.cw.dto.*;
import com.cw.entity.*;
import com.cw.mapper.SysUserMapper;
import com.cw.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.util.*;
import com.github.pagehelper.*;
import com.github.pagehelper.util.StringUtil;
import com.mchange.lang.*;
import java.util.*;
import javax.annotation.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author smq
 * @since 2021-04-28
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Resource
    private SysUserMapper sysUserMapper;
    /**
     * 获取用户通过用户主键或者登录名
     * @param userId   用户主键
     * @param userName 用户登录账号
     * @return java.util.List<com.cw.entity.SysUser>
     * @author ly
     * @date 2021/4/28 11:02
     */
    @Override
    public List<SysUser> getSysUser(String userId, String userName) {
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtil.isNotEmpty(userId)) { lambdaQueryWrapper.eq(SysUser::getUserId, userId); }
        if (StringUtil.isNotEmpty(userName)) { lambdaQueryWrapper.eq(SysUser::getUsername, userName); }

        lambdaQueryWrapper.eq(SysUser::getDelFlag, "0");
        return list(lambdaQueryWrapper);
    }

    /**
     * 修改用户表里的角色Code为NULL
     * @param userId 用户主键
     * @author ly
     * @date 2020/10/21 14:43
     */
    @Override
    public void updateSysUserSetRoleCodeNull(String userId) {
        baseMapper.updateRoleCodeSetNull(userId);
    }

    /**
     * 插入用户
     * @param sysUser 用户
     * @return com.cw.common.api.ResultObject
     * @author ly
     * @date 2021/4/28 11:48
     */
    @Override
    public ResultObject insertSysUser(SysUser sysUser) {
        String password = sysUser.getPassword();
        String passwd = SecureUtil.md5(password);
        sysUser.setPassword(passwd);
        save(sysUser);
        return ResultObject.successReturn("创建成功!");
    }

    /**
     * 修改用户表里的角色
     * @param sysUser 用户信息
     * @author ly
     * @date 2020/10/21 14:43
     */
    @Override
    public void updateSysUserSetRoleCode(SysUser sysUser) {
        baseMapper.updateById(sysUser);
    }

    /**
     * 根据菜单Id查询拥有此权限的用户
     * @param menuId
     * @return
     */
    @Override
    public List<SysUser> getUsersByMenuId(String menuId) {

        return null;
    }

    /**
     * 更新用户信息
     * @param
     * @return
     */
    @Override
    public boolean updateSysUser(SysUser sysUser) {
        return updateById(sysUser);
    }

    /**
     * 根据前台传输的条件进行查询
     * @param condition
     * @param sysUser
     * @return
     */
    @Override
    public ResultObject selectSysUserByCondition(SystemLogCondition condition, SysUser sysUser){
        HashMap<String, Object> map=new HashMap<>();
        if (sysUser==null||condition.getPageNum()==null||condition.getPageSize()==null){
            return  ResultObject.failed("请输入查询条件");
        }
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        if (sysUser.getUserId()!=null&&sysUser.getUserId()!=""){
            queryWrapper.eq("USER_ID", sysUser.getUserId());
        }
        if (sysUser.getUsername()!=null&&sysUser.getUsername()!=""){
            queryWrapper.eq("USERNAME", sysUser.getUsername());
        }
        if (sysUser.getRoleName()!=null&&sysUser.getRoleName()!=""){
            queryWrapper.eq("ROLE_NAME", sysUser.getRoleName());
        }
        if (sysUser.getEnable()!=null&&sysUser.getEnable()!=""){
            queryWrapper.eq("ENABLE", sysUser.getEnable());
        }
        if (StringUtil.isNotEmpty(sysUser.getDutyName())){
            queryWrapper.eq("DUTY_NAME", sysUser.getDutyName());
        }
        if (com.cw.util.StringUtil.isNotEmpty(sysUser.getDeptName())){
            queryWrapper.eq("DEPT_NAME", sysUser.getDeptName());
        }
        queryWrapper.eq("del_Flag", "0").orderByDesc("create_time");
        PageHelper.startPage(condition.getPageNum(),condition.getPageSize());
        List<SysUser> sysUsers=sysUserMapper.selectList(queryWrapper);
        return ResultObject.successReturn(new PageInfo<>(sysUsers));
    }


    /**
     * 无条件查询用户总数据
     * @param condition
     * @return
     */
    @Override
    public PageInfo<SysUser> sysUserSelectList(SystemLogCondition condition) {
        PageHelper.startPage(condition.getPageNum(), condition.getPageSize(),true);
        final List<SysUser> list = baseMapper.getSysUsers(condition);
        return new PageInfo<>(list);
    }

}
